﻿Partial Public Class SalesOrderExplorer
    ''' <summary>
    ''' GenerateInvoiceNumber
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub GenerateInvoiceNumber(ByVal sender As System.Object, ByVal e As EventArgs)


        Try
            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
            If String.IsNullOrEmpty(aDataRow("InvoiceNo").ToString) Then
                SystemBoxSDK.PL.CRMSDK.SalesOrder_PL.GenerateInvoiceNumber(aDataRow)
                MessageWindow.ShowMessage("Invoice No Generated", MsgBoxStyle.Information)
            Else
                MessageWindow.ShowMessage("Invoice No already Generated", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try


    End Sub

    ''' <summary>
    ''' Link Timesheet
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub LinkTimesheet(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then


            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Try
                ' Need to review Logic : B.Desjardins
                'If Not String.IsNullOrEmpty(aDataRow("InvoiceNo").ToString) Then

                Dim frm As SetInvoicedTimecardForm = New SetInvoicedTimecardForm() With {.ParamOrderNo = CType(aDataRow("SalesOrderId"), Guid), .ParamCustID = CType(aDataRow("CustomerId_FK"), Guid)}

                Dim result As DialogResult = frm.ShowDialog()

                If result = DialogResult.OK Then
                    MessageWindow.ShowMessage("Timesheet invoiced")
                Else
                    'error occured
                End If

                'Else
                '    MessageWindow.ShowMessage("You must first Generate Invoice No", MsgBoxStyle.Information)
                'End If

            Catch ex As Exception
                MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
            End Try


        Else
            'If MessageWindow.ShowMessage("Please Connect to a Database") = DialogResult.OK Then
            '    LoadLoginForm()
            'End If
        End If
    End Sub

    ''' <summary>
    ''' Unlink Timesheet
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub UnlinkTimesheet(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then

            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
            Dim saleOrderID As Guid = CType(aDataRow("SalesOrderId"), Guid)
            Dim statusID_FK As Guid = CType(aDataRow("StatusID_FK"), Guid)


            If IsDBNull(statusID_FK) OrElse statusID_FK = IdRepository.APP_SOSTATUS_NEW OrElse statusID_FK = IdRepository.APP_SOSTATUS_OPEN OrElse statusID_FK = IdRepository.APP_SOSTATUS_CANCEL OrElse statusID_FK = IdRepository.APP_SOSTATUS_HOLD Then
                'Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

                Try

                    SystemBoxSDK.PL.CRMSDK.SalesOrder_PL.UnlinkTimesheet(saleOrderID)

                    'sqlcmd = MetaQuery.GetSPCmdByMetaName("TimeDetail_Unlink")

                    'sqlcmd.Parameters("@OrderID").Value = saleOrderID
                    'sqlcmd.Parameters("@StatusID").Value = IdRepository.APP_TIMESHEETHOURSTATUS_TOINVOICE

                    'sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

                    'ReloadActiveGrid()
                Catch sqlex As System.Data.SqlClient.SqlException
                    MessageWindow.ShowMessage(sqlex)
                    'Finally
                    '    If sqlcmd IsNot Nothing Then
                    '        sqlcmd.Connection.Close()
                    '        sqlcmd.Dispose()
                    '    End If
                End Try
            Else
                MessageWindow.ShowMessage("Cannot unlink timesheet, sale order already invoiced.", MsgBoxStyle.Exclamation)
            End If
        End If
    End Sub

    ''' <summary>
    ''' PrintGOSaleOrder
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub PrintGOSaleOrder(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then

            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Dim Report As Invoice = New Invoice() With {.ParamOrderNo = aDataRow("OrderNo").ToString, .IsGO = True}
            Report.CreateDocument()

            Dim options As ExportOptions = Report.PrintingSystem.ExportOptions



            ' Set Print Preview options.
            options.PrintPreview.ActionAfterExport = ActionAfterExport.AskUser

            'Outstanding 
            'options.PrintPreview.DefaultDirectory =
            options.PrintPreview.DefaultFileName = String.Format("{0} {1} {2}", "SCIEF GO", Report.Name, Report.GetCurrentColumnValue("OrderNo").ToString)
            'options.PrintPreview.SaveMode = SaveMode.UsingDefaultPath
            options.PrintPreview.ShowOptionsBeforeExport = True

            ' Set E-mail options.
            options.Email.RecipientAddress = "someone@somewhere.com"
            options.Email.RecipientName = "Someone"
            options.Email.Subject = "Test"
            options.Email.Body = "Test"

            ' Set CSV-specific export options.
            options.Csv.Encoding = Encoding.Unicode
            options.Csv.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToString

            ' Set HTML-specific export options.
            options.Html.CharacterSet = "UTF-8"
            options.Html.RemoveSecondarySymbols = False
            options.Html.Title = "Test Title"

            ' Set Image-specific export options.
            options.Image.Format = ImageFormat.Jpeg

            ' Set MHT-specific export options.
            options.Mht.CharacterSet = "UTF-8"
            options.Mht.RemoveSecondarySymbols = False
            options.Mht.Title = "Test Title"

            ' Set PDF-specific export options.
            options.Pdf.Compressed = True
            options.Pdf.DocumentOptions.Application = AppManager.DbName
            options.Pdf.DocumentOptions.Author = AppManager.UserName
            options.Pdf.DocumentOptions.Keywords = String.Format("{0} {1}", Report.Name, Report.GetCurrentColumnValue("OrderNo").ToString)
            options.Pdf.DocumentOptions.Subject = "Test Subject"
            options.Pdf.DocumentOptions.Title = "Test Title"

            ' Set Text-specific export options.
            options.Text.Encoding = Encoding.Unicode
            options.Text.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator

            ' Set XLS-specific export options.
            options.Xls.ShowGridLines = True
            options.Xls.SheetName = "Page 1"
            options.Xls.TextExportMode = TextExportMode.Value

            ' Set XLSX-specific export options.
            options.Xlsx.ShowGridLines = True
            options.Xlsx.SheetName = "Page 1"
            options.Xlsx.TextExportMode = TextExportMode.Value



            'Dim aFrm As Form = New ReportPrintForm() With {.MdiParent = Me, .Text = "Print Sale Order", .RptPrintingSystem = Report.PrintingSystem}
            Dim aFrm As Form = New ReportPrintForm() With {.Text = "Print Sale Order", .Report = Report}
            aFrm.Show()

        Else
            'If MessageWindow.ShowMessage("Please Connect to a Database") = DialogResult.OK Then
            '    LoadLoginForm()
            'End If
        End If
    End Sub




    ''' <summary>
    ''' PrintInvoice
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub PrintInvoice(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then

            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Dim Report As Invoice = New Invoice() With {.ParamOrderNo = aDataRow("OrderNo").ToString}
            Report.CreateDocument()

            Dim options As ExportOptions = Report.PrintingSystem.ExportOptions

            ' Set Print Preview options.
            options.PrintPreview.ActionAfterExport = ActionAfterExport.AskUser

            'Outstanding 
            'options.PrintPreview.DefaultDirectory =
            options.PrintPreview.DefaultFileName = String.Format("{0} {1} {2}", Report.GetCurrentColumnValue("SupplierName").ToString, Report.Name, Report.GetCurrentColumnValue("OrderNo").ToString)
            'options.PrintPreview.SaveMode = SaveMode.UsingDefaultPath
            options.PrintPreview.ShowOptionsBeforeExport = True

            ' Set E-mail options.
            options.Email.RecipientAddress = "someone@somewhere.com"
            options.Email.RecipientName = "Someone"
            options.Email.Subject = "Test"
            options.Email.Body = "Test"

            ' Set CSV-specific export options.
            options.Csv.Encoding = Encoding.Unicode
            options.Csv.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToString()

            ' Set HTML-specific export options.
            options.Html.CharacterSet = "UTF-8"
            options.Html.RemoveSecondarySymbols = False
            options.Html.Title = "Test Title"

            ' Set Image-specific export options.
            options.Image.Format = ImageFormat.Jpeg

            ' Set MHT-specific export options.
            options.Mht.CharacterSet = "UTF-8"
            options.Mht.RemoveSecondarySymbols = False
            options.Mht.Title = "Test Title"

            ' Set PDF-specific export options.
            options.Pdf.Compressed = True
            options.Pdf.DocumentOptions.Application = AppManager.DbName
            options.Pdf.DocumentOptions.Author = AppManager.UserName
            options.Pdf.DocumentOptions.Keywords = String.Format("{0} {1}", Report.Name, Report.GetCurrentColumnValue("OrderNo").ToString)
            options.Pdf.DocumentOptions.Subject = "Test Subject"
            options.Pdf.DocumentOptions.Title = "Test Title"

            ' Set Text-specific export options.
            options.Text.Encoding = Encoding.Unicode
            options.Text.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator

            ' Set XLS-specific export options.
            options.Xls.ShowGridLines = True
            options.Xls.SheetName = "Page 1"
            options.Xls.TextExportMode = TextExportMode.Value

            ' Set XLSX-specific export options.
            options.Xlsx.ShowGridLines = True
            options.Xlsx.SheetName = "Page 1"
            options.Xlsx.TextExportMode = TextExportMode.Value

            'Dim aFrm As Form = New ReportPrintForm() With {.MdiParent = Me, .Text = "Print Invoice", .RptPrintingSystem = Report.PrintingSystem}
            Dim aFrm As Form = New ReportPrintForm() With {.Text = "Print Invoice", .Report = Report}
            aFrm.Show()


        Else
            'If MessageWindow.ShowMessage("Please Connect to a Database") = DialogResult.OK Then
            '    LoadLoginForm()
            'End If
        End If
    End Sub


    ''' <summary>
    ''' PrintSaleOrder
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub PrintSaleOrder(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then

            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Dim Report As SalesOrderReport = New SalesOrderReport() With {.ParamOrderNo = aDataRow("OrderNo").ToString}

            Report.CreateDocument()

            'Dim options As ExportOptions = Report.PrintingSystem.ExportOptions

            '' Set Print Preview options.
            'options.PrintPreview.ActionAfterExport = ActionAfterExport.AskUser

            ''Outstanding 
            ''options.PrintPreview.DefaultDirectory =
            'options.PrintPreview.DefaultFileName = Report.GetCurrentColumnValue("SupplierName").ToString & Report.Name
            'options.PrintPreview.SaveMode = SaveMode.UsingDefaultPath
            'options.PrintPreview.ShowOptionsBeforeExport = True

            '' Set E-mail options.
            'options.Email.RecipientAddress = "someone@somewhere.com"
            'options.Email.RecipientName = "Someone"
            'options.Email.Subject = "Test"
            'options.Email.Body = "Test"

            '' Set CSV-specific export options.
            'options.Csv.Encoding = Encoding.Unicode
            'options.Csv.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToString()

            '' Set HTML-specific export options.
            'options.Html.CharacterSet = "UTF-8"
            'options.Html.RemoveSecondarySymbols = False
            'options.Html.Title = "Test Title"

            '' Set Image-specific export options.
            'options.Image.Format = ImageFormat.Jpeg

            '' Set MHT-specific export options.
            'options.Mht.CharacterSet = "UTF-8"
            'options.Mht.RemoveSecondarySymbols = False
            'options.Mht.Title = "Test Title"

            '' Set PDF-specific export options.
            'options.Pdf.Compressed = True
            'options.Pdf.DocumentOptions.Application = AppManager.DbName
            'options.Pdf.DocumentOptions.Author = AppManager.UserName
            'options.Pdf.DocumentOptions.Keywords = String.Format("{0} {1}", Report.Name, Report.GetCurrentColumnValue("OrderNO").ToString)
            'options.Pdf.DocumentOptions.Subject = "Test Subject"
            'options.Pdf.DocumentOptions.Title = "Test Title"

            '' Set Text-specific export options.
            'options.Text.Encoding = Encoding.Unicode
            'options.Text.Separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator

            '' Set XLS-specific export options.
            'options.Xls.ShowGridLines = True
            'options.Xls.SheetName = "Page 1"
            'options.Xls.TextExportMode = TextExportMode.Value

            '' Set XLSX-specific export options.
            'options.Xlsx.ShowGridLines = True
            'options.Xlsx.SheetName = "Page 1"
            'options.Xlsx.TextExportMode = TextExportMode.Value





            'Dim aFrm As Form = New ReportPrintForm() With {.MdiParent = Me, .Text = "Print Sale Order", .RptPrintingSystem = Report.PrintingSystem}
            Dim aFrm As Form = New ReportPrintForm() With {.Text = "Print Sale Order", .Report = Report}
            aFrm.Show()

        Else
            'If MessageWindow.ShowMessage("Please Connect to a Database") = DialogResult.OK Then
            '    LoadLoginForm()
            'End If
        End If


    End Sub

    ''' <summary>
    ''' PrintTimeDetail
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Overridable Sub PrintTimeDetail(ByVal sender As System.Object, ByVal e As EventArgs)
        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then

            Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Dim Report As TimeSheetHoursReport = New TimeSheetHoursReport() With {.ParamOrderNo = aDataRow("OrderNo").ToString}
            Report.CreateDocument()
            'Dim aFrm As Form = New ReportPrintForm() With {.MdiParent = Me, .Text = "Print Time Details", .RptPrintingSystem = Report.PrintingSystem}
            Dim aFrm As Form = New ReportPrintForm() With {.Text = "Print Time Details", .Report = Report}
            aFrm.Show()

        Else
            'If MessageWindow.ShowMessage("Please Connect to a Database") = DialogResult.OK Then
            '    LoadLoginForm()
            'End If
        End If
    End Sub

End Class
